package com.match.competition_role.controller;

import com.alibaba.fastjson.JSONObject;
import com.match.commonutils.Result;
import com.match.competition_role.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author 芊薰
 */
@Api(tags = "登录模块")
@RestController
@RequestMapping("/admin/competition_role/index")
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 根据token获取用户信息
     */
    @ApiOperation("根据token获取用户信息")
    @GetMapping("/getInfo")
    public Result getInfo(){
        //获取当前登录用户信息
        String number = SecurityContextHolder.getContext().getAuthentication().getName();
        Map<String, Object> userInfo = loginService.getUserInfo(number);
        if (userInfo.size()<0){
            return Result.error().message("用户信息获取失败请重新登录");
        }
        return Result.ok().data(userInfo);
    }

    /**
     * 获取权限
     * @return
     */
    @ApiOperation("获取权限")
    @GetMapping("/getPermission")
    public Result getMenu(){
        //获取当前登录用户用户名
        String number = SecurityContextHolder.getContext().getAuthentication().getName();
        List<JSONObject> permissionList = loginService.getPermission(number);
        return Result.ok().data("permissionList", permissionList);
    }


    /**
     * 用户退出
     * @return
     */
    @ApiOperation("用户退出")
    @PostMapping("/logout")
    public Result logout(){
        return Result.ok();
    }
}
